// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - WHICH ONE AM I
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;

body;

beginstate INIT_STATE;
	set_level(ME,28);
	set_boss_level(ME,2);
	set_name(ME,"Servile Infiltrator");
	if (get_memory_cell(0) == 0)
		set_name(ME,"Parla");
		
	if (gf(101,5) > 0)
		erase_char(ME);
	if (gf(76,5) > 1)
		erase_char(ME);
	break;

beginstate DEAD_STATE;
	inc_flag(76,20 + get_memory_cell(0),1);
break;

beginstate START_STATE;
	if ((get_attitude(ME) < 10) && (gf(76,5) < 2) && (num_chars_in_group(4) < 4))
		set_attitude(ME,10);
		
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	if (gf(76,5) > 1)
			set_state(4);


	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((get_attitude(ME) < 10) && (gf(76,5) < 2) && (num_chars_in_group(4) < 4))
		set_attitude(ME,10);

	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate 4; // patrol
	if (dist_to_nav_point(ME,0) <= 2) {
		print_named_str(ME,"escapes into Kayar's Spire.");
		inc_flag(76,24 + get_memory_cell(0),1);
		erase_char(ME);
		}
	follow_path(ME,0,0);
break;

beginstate TALKING_STATE;

	if (gf(76,5) > 1) {
		print_str("Talking: The servile is too busy to talk right now.");
		}
		else if (get_memory_cell(0) == 0)
			begin_talk_mode(30);
			else begin_talk_mode(5);	
break;